//
//  WBTitleButton.swift
//  Weibo
//
//  Created by yinbo on 2018/3/15.
//  Copyright © 2018年 yinbo. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {
    
    init(title:String?) {
        super.init(frame: CGRect())
        if title == nil {
            setTitle("首页", for: [])
        }else{
            setTitle(title!, for: [])
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
        }
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor( UIColor.darkGray, for: [])
        //设置大小
        sizeToFit()
        
    }
    //重新布局子 视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //判断titlelable,imageview是否同时存在
        guard let titleLabel = titleLabel,let imageView = imageView else{
            return
        }
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        titleLabel.frame.origin.x = 0
        
        // 将 imageView 的 x 向右移动 label 的宽度
        imageView.frame.origin.x = titleLabel.bounds.width
        print("调整按钮布局")
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implement")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
